/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package SysAguas.controle;

import SysAguas.dao.DAOHospede;
import SysAguas.modelo.Hospede;
import SysAguas.visao.DialogCadastrarHospede;
import SysAguas.visao.PanelCadastrarReserva;
import SysAguas.visao.TelaPrincipal;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author Heitor
 */
public class ReservaServico implements ActionListener {

    private PanelCadastrarReserva panelCadastro = null;
    private DialogCadastrarHospede cadastrarHospedeDialog;

    public ReservaServico(PanelCadastrarReserva panel) {
        this.panelCadastro = panel;
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == panelCadastro.getCadastrar_novo()) {
            Frame[] frame = JFrame.getFrames();

            cadastrarHospedeDialog = new DialogCadastrarHospede(frame[0], "Cadastrar novo Hospede", true);
            cadastrarHospedeDialog.montarJanelaCadastro();

        }
        if (e.getSource() == panelCadastro.getFechar()) {
            panelCadastro.setVisible(false);
            Frame[] frame = JFrame.getFrames();
            TelaPrincipal frame1 = (TelaPrincipal) frame[0];
            frame1.add(frame1.getPanelPrincipal(), BorderLayout.CENTER);
            frame1.getPanelPrincipal().setVisible(true);

        }
        if (e.getSource() == panelCadastro.getPesquisarNome()) {
            DAOHospede dao = new DAOHospede();
            List<Hospede> list = dao.getHospedesByName(panelCadastro.getPesquisar_NOme().getText());
            if (list.size() == 0) {
                JOptionPane.showMessageDialog(panelCadastro, "Hospede Não Cadastrado:\n" + panelCadastro.getPesquisar_NOme().getText());
            }

            JTable table = panelCadastro.getJtable2();


            DefaultTableModel tableModel = (DefaultTableModel) table.getModel();
            System.out.println("Howsssss ==" + tableModel.getRowCount());
            int rows = tableModel.getRowCount();
            

            for (int i = 0; i < rows; i++) {
                tableModel.removeRow(0);
            }
            for (Hospede hospede : list) {
                tableModel.addRow(new Object[]{hospede.getNome(), hospede.getSobrenome()});

            }
            table.setModel(tableModel);
            panelCadastro.setJtable2(table);

        }

    }
}
